package com.sunwoda.springbootshiro.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @program: springboot-shiro
 * @description: AOP  参考csdn：https://www.cnblogs.com/joy99/p/10941543.html
 * @author: SGR
 * @create: 2021-09-16 11:35
 **/
@org.aspectj.lang.annotation.Aspect
@Component
@Slf4j
public class Aspect {

    /**
    * @Description: 前置通知
    * @Param:
    * @return:
    * @Author: SGR
    * @Date: 2021-09-16
    */
    @Before("execution(* com.sunwoda.springbootshiro.service.impl.*.*(..))")
    public void before(){
        log.info("前置通知");
    }

    /**
    * @Description: 后置通知
    * @Param:
    * @return:
    * @Author: SGR
    * @Date: 2021-09-16
    */
    @After("execution(* com.sunwoda.springbootshiro.service.impl.*.*(..))")
    public void after(){
        log.info("后置通知");
    }

    /**
    * @Description: 环绕通知 环绕通知需要调用方法，然后在调用方法的前后定义前置通知和后置通知，
     * 通过ProceedingJoinPoint类调用方法，如果没有调用方法，就不会执行方法
     * 环绕通知需要调用了方法，需要我们手动返回返回值
    * @Param:
    * @return:
    * @Author: SGR
    * @Date: 2021-09-16
    */
    @Around("execution(* com.sunwoda.springbootshiro.service.impl.*.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("环绕前置通知通知");
        Object proceed = proceedingJoinPoint.proceed();
        log.info("环绕后置通知");
        return proceed;
    }

    /**
    * @Description: 返回后通知，即在目标方法返回后调用
    * @Param:
    * @return:
    * @Author: SGR
    * @Date: 2021-09-16
    */
    @AfterReturning("execution(* com.sunwoda.springbootshiro.service.impl.*.*(..))")
    public void afterReturn(){
        log.info("返回后通知");
    }

    /**
    * @Description: 异常通知
    * @Param:
    * @return:
    * @Author: SGR
    * @Date: 2021-09-16
    */
    @AfterThrowing("execution(* com.sunwoda.springbootshiro.service.impl.*.*(..))")
    public void afterThrow(){
        log.info("异常通知");
    }

}
